
<template>
  <div class="dashboard-container">
    <el-drawer ref="drawer" size="600px" :title="status==1?'新增':'编辑'" :visible.sync="showDialog" :wrapper-closable="true" @close="close">
      <div class="detial">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
          <el-form-item label="版本号:" prop="vision_no">
            <el-input v-model="ruleForm.vision_no" placeholder="请输入版本号" />
          </el-form-item>
          <el-form-item label="更新内容:" prop="desc">
            <el-input v-model="ruleForm.desc" type="textarea" placeholder="请输入更新内容" />
          </el-form-item>

          <el-form-item label="上传安装包:" prop="upload">
            <el-upload
              class="avatar-uploader"
              :show-file-list="false"
              drag
              :headers="headers"
              :action="orderUploadUrl"
              :on-success="handleOrderUpload"
              :multiple="false"
            >
              <i class="el-icon-upload" />
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            </el-upload>
          </el-form-item>
          <el-form-item label="下载地址:" prop="url">
            <el-input v-model="ruleForm.url" placeholder="请输入下载地址" />
          </el-form-item>
        </el-form>
      </div>
      <div class="buttons">
        <el-button
          v-if="status == 1"
          size="mini"
          type="primary"
          style="margin-left:20px"
          @click="addNews()"
        >
          新 增
        </el-button>
        <el-button
          v-if="status == 2"
          size="mini"
          type="primary"
          style="margin-left:20px"
          @click="editNews()"
        >
          保 存
        </el-button>
        <el-button
          size="mini"
          type=""
          @click="close()"
        >
          取消
        </el-button>

      </div>

    </el-drawer>
  </div>
</template>

<script>
import { CreateVision, UpdateVision } from '@/api/PDA/index'
import { getToken } from '@/utils/auth'
import service from '@/utils/request'

export default {
  name: 'Detial',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    detial: {
      require: false,
      type: Object,
      default: () => {

      }
    },
    status: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      headers: {
        gtAdminToken: getToken()
      },
      orderUploadUrl: process.env.VUE_APP_BASE_API + '/user/uploadImg',
      time: '',
      showDialog: false,
      OutletList: [],
      ruleForm: {
        'vision_no': '',
        'desc': '',
        'url': ''
      },
      rules: {
        vision_no: [
          { required: true, message: '请输入版本号', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '请输入更新内容' }
        ],
        url: [
          { required: true, message: '请输入下载地址' }
        ]
      }
    }
  },
  watch: {
    visible(val) {
      this.showDialog = this.visible
      if (!this.showDialog) {
        this.resetForm()
      } else {
        if (this.status === 2) {
          Object.keys(this.ruleForm).forEach(i => {
            this.ruleForm[i] = this.detial[i]
          })
          this.ruleForm.id = this.detial.id
        }
      }
    }
  },
  mounted() {
    console.log(service.baseURL)
  },
  methods: {
    handleOrderUpload(res) {
      var type = (res.code === 200) ? 'success' : 'info'
      if (type === 'success') {
        this.ruleForm.url = res.data.url
      }
      this.$message.success({
        message: res.msg,
        type: type
      })
    },
    close() {
      this.$emit('update', false)
      this.ruleForm = {
        'vision_no': '',
        'desc': '',
        'url': ''
      }
    },
    resetForm() {
      this.$refs['ruleForm'].resetFields()
    },
    addNews() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          CreateVision(this.ruleForm).then(res => {
            this.$message.success('添加成功')
            this.$emit('reload')
          })
        } else {
          return false
        }
      })
    },
    editNews() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          UpdateVision(this.ruleForm).then(res => {
            this.$message.success('编辑成功')
            this.$emit('reload')
          })
        } else {
          return false
        }
      })
    }

  }

}
</script>
<style scoped>
.detial{
  padding:40px 40px 20px 20px;
}
.buttons{
  display: flex;
  flex-direction: row-reverse;
  padding-right: 40px;
}
</style>
